package com.net.xpay.common.manager.impl;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.xpay.common.dao.SafePayBookPreApplyRecordMapper;
import com.net.xpay.common.domain.SafePayBookPreApplyRecord;
import com.net.xpay.common.manager.SafePayBookPreApplyRecordManager;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.model.search.SafePayBookPreApplyRecordSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class SafePayBookPreApplyRecordManagerImpl extends AbstractManager<SafePayBookPreApplyRecord> implements SafePayBookPreApplyRecordManager{

    @Autowired
    private SafePayBookPreApplyRecordMapper mapper;


    @Override
    protected BasicMapper<Long, SafePayBookPreApplyRecord> getMapper() {
        return mapper;
    }

    @Override
    public SafePayBookPreApplyRecord getByToken(String token) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(token));

        SafePayBookPreApplyRecord condition = new SafePayBookPreApplyRecord();
        condition.setApplyToken(token);

        return getByCondition(condition);
    }

    @Override
    public List<SafePayBookPreApplyRecord> search(SafePayBookPreApplyRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(SafePayBookPreApplyRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public SafePayBookPreApplyRecord getLastByAlipayName(String alipayName, String alipayNickName) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(alipayName) && !Strings.isNullOrEmpty(alipayNickName));

        SafePayBookPreApplyRecord condition = new SafePayBookPreApplyRecord();
        condition.setAlipayName(alipayName);
        condition.setAlipayNickName(alipayNickName);

        return mapper.getLastByCondition(condition);
    }

    @Override
    public SafePayBookPreApplyRecord getByExternalAgreementNo(String externalAgreementNo) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(externalAgreementNo));

        SafePayBookPreApplyRecord condition = new SafePayBookPreApplyRecord();
        condition.setExternalAgreementNo(externalAgreementNo);

        return getByCondition(condition);
    }

    @Override
    public List<SafePayBookPreApplyRecord> list(SafePayBookPreApplyRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }
}
